﻿options nolabel;

proc import out=work.test16 datafile="I:/prepare/pseudo_data.dta" dbms=dta replace;run;  
proc import out=work.crypto_factor datafile="I:/prepare/crypto_factor.dta" dbms=dta replace;run;  


data umd; set test16;if year= 2019 or (year=2018 and month>=8) or year=2020 or  year=2021;

run;
proc means data=umd; var ab_turn_daily_new_30; run;
%macro DO_1(termlist);

%ODSOff();


  %local i stc_term ;
  %do i=1 %to %sysfunc(countw(&termlist,%str( )));
    %let STC_TERM=%scan(&termlist,&i,%str( ));


proc sort data=umd; by date; run;
proc rank data=umd out=umd2 group=3;
  by date;
    var &STC_TERM;
    ranks cv_rank;
run;

data umd3; set umd2;  
cv_rank=cv_rank+1;   
run;

proc sort data=umd3; by date cv_rank; run;
proc rank data=umd3 out=umd4 group=5;
  by date cv_rank;
    var ab_turn_daily_new_30;
    ranks ABVOL_rank;
run;

data umd5; set umd4;
ABVOL_rank=ABVOL_rank+1;

if cv_rank=1 and ABVOL_rank=1 then momr=1 ;
if cv_rank=1 and ABVOL_rank=2 then momr=2 ;
if cv_rank=1 and ABVOL_rank=3 then momr=3 ;
if cv_rank=1 and ABVOL_rank=4 then momr=4 ;
if cv_rank=1 and ABVOL_rank=5 then momr=5 ;

if cv_rank=2 and ABVOL_rank=1 then momr=6 ;
if cv_rank=2 and ABVOL_rank=2 then momr=7 ;
if cv_rank=2 and ABVOL_rank=3 then momr=8 ;
if cv_rank=2 and ABVOL_rank=4 then momr=9 ;
if cv_rank=2 and ABVOL_rank=5 then momr=10 ;

if cv_rank=3 and ABVOL_rank=1 then momr=11 ;
if cv_rank=3 and ABVOL_rank=2 then momr=12 ;
if cv_rank=3 and ABVOL_rank=3 then momr=13 ;
if cv_rank=3 and ABVOL_rank=4 then momr=14 ;
if cv_rank=3 and ABVOL_rank=5 then momr=15 ;

run;

proc sort data=umd5 ; by date momr ; run;

proc means data = umd5 noprint;
  by date momr;
    var holding_ret_daily;
      output out = ewretdat mean= ewret std = ewretstd;
run;

data ewretdat1; set ewretdat; 

if missing(momr) then delete;
rank=momr;
rank_id=cats('PORT',rank);
 run;

proc sort data=ewretdat1; by date rank; run;
proc transpose data=ewretdat1 out=ewretdat2 ;

by date;id rank_id;var ewret;
run;

Data gout; set ewretdat2;

PORT_1=(PORT1+PORT6+PORT11)/3;
PORT_2=(PORT2+PORT7+PORT12)/3;
PORT_3=(PORT3+PORT8+PORT13)/3;
PORT_4=(PORT4+PORT9+PORT14)/3;
PORT_5=(PORT5+PORT10+PORT15)/3;

PORT1=PORT_1;
PORT2=PORT_2;
PORT3=PORT_3;
PORT4=PORT_4;
PORT5=PORT_5;


ret_diff=PORT5-PORT1;

holding_date = intnx("day",date, 1,"e");
format holding_Date MMDDYY10.;
run;

proc means data=gout n mean t probt std;var PORT1 PORT2 PORT3 PORT4 PORT5 
ret_diff ;run;

proc sql;
	create table gout2 as select a.*, b.cmkt, b.csmb, b.cmom, b.rf_daily
		from gout as a left join crypto_factor as b on 
		a.holding_date=b.date_temp;
	run;

data ff4; set gout2;     run;
data ff4; set ff4;
PORT1_rf=PORT1-rf_daily;
PORT2_rf=PORT2-rf_daily;
PORT3_rf=PORT3-rf_daily;
PORT4_rf=PORT4-rf_daily;
PORT5_rf=PORT5-rf_daily;

ret_diff=port5_rf-port1_rf;
year=year(date);
month=month(date);
run;


proc means data=ff4 n mean t probt std;var PORT1_rf PORT2_rf PORT3_rf PORT4_rf PORT5_Rf ret_diff; run;


%let lag=8;
proc model data=ff4; parms a1  ;exogenous PORT1_rf; instruments / intonly;PORT1_rf= a1 ; fit PORT1_rf / gmm kernel=(bart,%eval(&lag+1), 0);ods output parameterestimates=PORT1_rf_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data PORT1 (keep=PORT1_param PORT1_T );set PORT1_rf_Excess_ret; port='PORT2_rf_3factor'; if parameter='a1'; 
	tvalue2=put(tvalue, 7.2);
	if probt<0.1 then p='*  ';
	if probt<0.05 then p='** ';
	if probt<0.01 then p='***';
	PARAM=compress(put(estimate, 7.3)||p);
	T=compress('('||tvalue2||')');
	
rename PARAM=PORT1_param T=PORT1_T;

run;

proc model data=ff4; parms a1  ;exogenous PORT2_rf ; instruments / intonly;PORT2_rf= a1 ; fit PORT2_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT2_rf_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data PORT2 (keep=PORT2_param PORT2_T );set PORT2_rf_Excess_ret; port='PORT2_rf_3factor'; if parameter='a1'; 
	tvalue2=put(tvalue, 7.2);
	if probt<0.1 then p='*  ';
	if probt<0.05 then p='** ';
	if probt<0.01 then p='***';
	PARAM=compress(put(estimate, 7.3)||p);
	T=compress('('||tvalue2||')');
	
rename PARAM=PORT2_param T=PORT2_T;

run;

proc model data=ff4; parms a1  ;exogenous PORT3_rf ; instruments / intonly;PORT3_rf= a1; fit PORT3_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT3_rf_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data PORT3 (keep=PORT3_param PORT3_T );set PORT3_rf_Excess_ret; port='PORT3_rf_3factor'; if parameter='a1'; 
	tvalue2=put(tvalue, 7.2);
	if probt<0.1 then p='*  ';
	if probt<0.05 then p='** ';
	if probt<0.01 then p='***';
	PARAM=compress(put(estimate, 7.3)||p);
	T=compress('('||tvalue2||')');
	
rename PARAM=PORT3_param T=PORT3_T;

run;
proc model data=ff4; parms a1  ;exogenous PORT4_rf; instruments / intonly;PORT4_rf= a1 ; fit PORT4_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT4_rf_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data PORT4 (keep=PORT4_param PORT4_T );set PORT4_rf_Excess_ret; port='PORT4_rf_3factor'; if parameter='a1'; 
	tvalue2=put(tvalue, 7.2);
	if probt<0.1 then p='*  ';
	if probt<0.05 then p='** ';
	if probt<0.01 then p='***';
	PARAM=compress(put(estimate, 7.3)||p);
	T=compress('('||tvalue2||')');
	
rename PARAM=PORT4_param T=PORT4_T;

run;

proc model data=ff4; parms a1  ;exogenous PORT5_rf ; instruments / intonly;PORT5_rf= a1 ; fit PORT5_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT5_rf_Excess_ret fitstatistics=fitresult OutputStatistics=residual;
data PORT5 (keep=PORT5_param PORT5_T );set PORT5_rf_Excess_ret; port='PORT5_rf_3factor'; if parameter='a1'; 
	tvalue2=put(tvalue, 7.2);
	if probt<0.1 then p='*  ';
	if probt<0.05 then p='** ';
	if probt<0.01 then p='***';
	PARAM=compress(put(estimate, 7.3)||p);
	T=compress('('||tvalue2||')');
	
rename PARAM=PORT5_param T=PORT5_T;

run;




proc model data=ff4; parms a1 ;exogenous ret_diff ; instruments / intonly;ret_diff= a1 ; fit ret_diff / gmm kernel=(bart,%eval(&lag+1),0);ods output parameterestimates=ret_diff_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data Excess (keep=Excess_PARAM Excess_T );
set ret_diff_Excess_ret; port='port';
if parameter='a1'; 
	tvalue2=put(tvalue, 7.2);
	if probt<0.1 then p='*  ';
	if probt<0.05 then p='** ';
	if probt<0.01 then p='***';
	PARAM=compress(put(estimate, 7.3)||p);
	T=compress('('||tvalue2||')');
	
rename PARAM=Excess_param T=Excess_T;
run;


proc model data=ff4; parms a1 b1 ;instruments cmkt ;ret_diff= a1 + b1*cmkt; fit ret_diff / gmm kernel=(bart,%eval(&lag+1), 0);ods output parameterestimates=ret_diff_CAPM  fitstatistics=fitresult OutputStatistics=residual;
data CAPM (keep=CAPM_PARAM CAPM_T );
set ret_diff_CAPM; port='port';
if parameter='a1';
	tvalue2=put(tvalue, 7.2);
	if probt<0.1 then p='*  ';
	if probt<0.05 then p='** ';
	if probt<0.01 then p='***';
		PARAM=compress(put(estimate, 7.3)||p);
	T=compress('('||tvalue2||')');

rename PARAM=CAPM_param T=CAPM_T;
run;


proc model data=ff4; parms a1 b1 b2 b3 ;instruments cmkt csmb cmom;ret_diff= a1 + b1*cmkt  + b2*csmb + b3*cmom; fit ret_diff / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=ret_diff_3factor  fitstatistics=fitresult OutputStatistics=residual;
data Three (keep=Three_PARAM Three_T );
set ret_diff_3factor; 
port='port';
if parameter='a1'; 
	tvalue2=put(tvalue, 7.2);
	if probt<0.1 then p='*  ';
	if probt<0.05 then p='** ';
	if probt<0.01 then p='***';
	PARAM=compress(put(estimate, 7.3)||p);
	T=compress('('||tvalue2||')');
	
rename PARAM=Three_param T=Three_T;
run;

data &STC_TERM; merge PORT1 PORT2 PORT3 PORT4 PORT5 Excess CAPM Three ; run;
  %end;
%mend DO_1;

%ODSOff();
%let var_list=
cv_turn reversal abs_reversal  mcap  mom_1_11  illiq IVOL ab_svi_median  max_daily 

;
%DO_1(&var_list);


data final_combined;
set cv_turn reversal abs_reversal  mcap  mom_1_11  illiq IVOL ab_svi_median  max_daily; 
run;


